# encoding: utf-8
"""
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    created by siyi.chen on '12/03/2025'
    comment: 消息通知
"""
from fastapi import APIRouter, Request, Body, Query

from config import Config
from project.core.function import json_response
from project.lib.decorator import api_common, exec_duration
from project.lib.validators import DateTimeStr, generate_validator
from project.service.message import MessageService
from project.service.record import RecordService


message_router = APIRouter(prefix='/u', tags=['user.message'])


@message_router.post('/message/push', summary='消息推送')
@api_common(doc_replace=(('{0}', Config.ENV_DOC_FIX),))
@exec_duration(add_return=True)
async def do_message_push(request: Request,
                          third_party_id: str = Body(..., title='第三方系统标识'),
                          third_party_msg_id: int = Body(..., title='第三方系统消息唯一ID'),
                          type: str = Body(..., title='消息类型 service(服务)/activity(活动)'),
                          icon: str = Body('', title='图标'),
                          title: str = Body('', title='消息标题'),
                          voice: str = Body('', title='语音播报内容'),
                          content: dict = Body({}, title='消息内容(JSON/文本)'),
                          expire_time: generate_validator(DateTimeStr, blank_enable=True) = Body('', title='消息有效期', description='格式: yyyy-mm-dd hh:mm:ss'),
                          session=None,
                          body_params=None):
    """
    **desc**: 消息推送

    **url**: http(s)://{0}.udicaria.com/u/message/push

    **method**: POST

    **params**:

        <body>:
            third_party_id: 第三方系统标识
            third_party_msg_id: 第三方系统消息唯一ID
            type: 消息类型 service(服务)/activity(活动)
            icon: 图标
            title: 消息标题
            voice: 语音播报
            content: 消息内容(JSON/文本)
            {
                flow_record_id: 流程记录ID
                role_name: 服务人员名称，与AIS角色名称一致
                role_phone: 角色手机号，如果为空，则从AIS配置中获取
                to_flow_process_id: 目标服务状态id
            } 服务通知内容
            {
                第三方系统自定义json数据
            } 活动通知内容
            expire_time: 消息有效期，格式: yyyy-mm-dd hh:mm:ss，为空则默认一天后过期

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data:
            exec_time: 接口后端处理时长
        }
    """
    if type == 'service':
        # 解析服务通知内容
        flow_record_id = content.get('flow_record_id')
        to_flow_process_id = content.get('to_flow_process_id')
        role_name = content.get('role_name')
        role_phone = content.get('role_phone')
        await RecordService.do_flow_process_jump(session, flow_record_id, to_flow_process_id, role_name, role_phone, voice)
    elif type == 'activity':
        if not icon:
            body_params.icon = 'https://roboland-ai.oss-cn-shenzhen.aliyuncs.com/leto/icon/activity.png'
        await MessageService.do_message_push(session, body_params)

    return json_response()
